package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceItem;
import com.eastfair.venueservice.enumeration.ServiceItemServiceStateEnum;
import com.eastfair.venueservice.service.ServiceItemService;
import com.eastfair.venueservice.vo.ServiceItemVO;
import com.eastfair.venueservice.vo.ServiceItemWithDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务项
 * </p>
 *
 * @author linan
 * @date 2022-07-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceItem")
@Api(value = "ServiceItem", tags = "服务项")
@PreAuth(replace = "venueservice:serviceItem:")
public class ServiceItemController extends SuperController<ServiceItemService, Long, ServiceItem, ServiceItemPageQuery, ServiceItemSaveDTO, ServiceItemUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServiceItem> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceItem> serviceItemList = list.stream().map((map) -> {
            ServiceItem serviceItem = ServiceItem.builder().build();
            //TODO 请在这里完成转换
            return serviceItem;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceItemList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceItemPageQuery> params) {
        ServiceItemPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加服务项
     *
     * @param serviceItemDTO 添加服务项
     * @return R
     */
    @ApiOperation(value = "添加服务项", notes = "添加服务项")
    @SysLog("添加服务项")
    @PostMapping("/addServiceItem")
    public R<Boolean> addServiceItem(@RequestBody @Validated(ServiceItemDTO.AddCheck.class) ServiceItemDTO serviceItemDTO) {
        log.info("addServiceItem - 添加服务项 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.addServiceItem(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 修改服务项
     *
     * @param serviceItemDTO 修改服务项
     * @return R
     */
    @ApiOperation(value = "修改服务项", notes = "修改服务项")
    @SysLog("修改服务项")
    @PostMapping("/updateServiceItem")
    public R<Boolean> updateServiceItem(@RequestBody @Validated(ServiceItemDTO.UpdateCheck.class) ServiceItemDTO serviceItemDTO) {
        log.info("updateServiceItem - 修改服务项 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.updateServiceItem(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 修改服务项库存
     *
     * @param serviceItemDTO 修改服务项库存
     * @return R
     */
    @ApiOperation(value = "修改服务项库存", notes = "修改服务项库存")
    @SysLog("修改服务项库存")
    @PostMapping("/updateServiceItemInventory")
    public R<Boolean> updateServiceItemInventory(@RequestBody ServiceItemDTO serviceItemDTO) {
        log.info("updateServiceItemInventory - 修改服务项库存 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.updateServiceItemInventory(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 服务上架
     *
     * @param serviceItemDTO 服务上架
     * @return R
     */
    @ApiOperation(value = "服务上架", notes = "服务上架")
    @SysLog("服务上架")
    @PostMapping("/shelfService")
    public R<?> shelfService(@RequestBody @Validated(ServiceItemDTO.Primary.class) ServiceItemDTO serviceItemDTO) {
        log.info("shelfService - 服务上架 serviceItemDTO={}", serviceItemDTO);
        serviceItemDTO.setServiceState(ServiceItemServiceStateEnum.ON_SHELVES);
        try{
            return baseService.updateServiceItemState(serviceItemDTO);
        }catch (Exception e){
            return R.fail(e);
        }
    }

    /**
     * 服务下架
     *
     * @param serviceItemDTO 服务下架
     * @return R
     */
    @ApiOperation(value = "服务下架", notes = "服务下架")
    @SysLog("服务下架")
    @PostMapping("/offShelfService")
    public R<?> offShelfService(@RequestBody @Validated(ServiceItemDTO.Primary.class) ServiceItemDTO serviceItemDTO) {
        log.info("offShelfService - 服务上架、下架 serviceItemDTO={}", serviceItemDTO);
        serviceItemDTO.setServiceState(ServiceItemServiceStateEnum.UNDER_SHELVES);
        try {
            return baseService.updateServiceItemState(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 删除服务
     *
     * @param serviceItemDTO 删除服务
     * @return R
     */
    @ApiOperation(value = "删除服务", notes = "删除服务")
    @SysLog("删除服务")
    @PostMapping("/delServiceItem")
    public R<?> delServiceItem(@RequestBody @Validated(ServiceItemDTO.Primary.class) ServiceItemDTO serviceItemDTO) {
        log.info("delServiceItem - 删除服务 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.delServiceItem(serviceItemDTO,false);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 查询服务项
     *
     * @param serviceItemQuery 查询服务项
     * @return R
     */
    @ApiOperation(value = "查询服务项", notes = "查询服务项")
    @PostMapping("/queryServiceItem")
    public R<IPage<ServiceItemVO>> queryServiceItem(@RequestBody PageParams<ServiceItemQuery> serviceItemQuery) {
        log.info("queryServiceItem - 查询服务项 serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItem(serviceItemQuery));
    }

    /**
     * 简单查询服务项列表（不分页）
     *
     * @param serviceItemQuery 简单查询服务项列表（不分页）
     * @return R
     */
    @ApiOperation(value = "简单查询服务项列表（不分页）", notes = "简单查询服务项列表（不分页）")
    @PostMapping("/queryServiceItemSimpleList")
    @BaseControllerInfo(voGroup = ServiceItemVO.Simple.class)
    public R<List<ServiceItemVO>> queryServiceItemSimpleList(@RequestBody ServiceItemQuery serviceItemQuery) {
        log.info("queryServiceItem - 简单查询服务项列表（不分页） serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItemSimpleList(serviceItemQuery));
    }

    /**
     * 搭建商准备预订服务的服务列表
     *
     * @param serviceItemQuery 搭建商准备预订服务的服务列表
     * @return R
     */
    @ApiOperation(value = "搭建商准备预订服务的服务列表", notes = "搭建商准备预订服务的服务列表")
    @PostMapping("/queryServiceItemOfSubscribe")
    public R<List<ServiceItemVO>> queryServiceItemOfSubscribe(@RequestBody PageParams<ServiceItemQuery> serviceItemQuery) {
        log.info("queryServiceItemOfSubscribe - 搭建商准备预订服务的服务列表 serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItemOfSubscribe(serviceItemQuery));
    }

    /**
     * 主运准备预订服务的服务列表
     *
     * @param serviceItemQuery 主运准备预订服务的服务列表
     * @return R
     */
    @ApiOperation(value = "主运准备预订服务的服务列表", notes = "主运准备预订服务的服务列表")
    @PostMapping("/queryServiceItemOfTransport")
    public R<List<ServiceItemVO>> queryServiceItemOfTransport(@RequestBody ServiceItemQuery serviceItemQuery) {
        log.info("queryServiceItemOfTransport - 主运准备预订服务的服务列表 serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItemOfTransport(serviceItemQuery));
    }

    /**
     * 主办准备预订服务的服务列表
     *
     * @param serviceItemQuery 主办准备预订服务的服务列表
     * @return R
     */
    @ApiOperation(value = "主办准备预订服务的服务列表", notes = "主办准备预订服务的服务列表")
    @PostMapping("/queryServiceItemOfSponsor")
    public R<List<ServiceItemVO>> queryServiceItemOfSponsor(@RequestBody ServiceItemQuery serviceItemQuery) {
        log.info("queryServiceItemOfTransport - 主办准备预订服务的服务列表 serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItemOfSponsor(serviceItemQuery));
    }

    /**
     * 场馆现场预订服务的服务列表
     *
     * @param serviceItemQuery 场馆现场预订服务的服务列表
     * @return R
     */
    @ApiOperation(value = "场馆现场预订服务的服务列表", notes = "场馆现场预订服务的服务列表")
    @PostMapping("/queryServiceItemOfVenue")
    public R<List<ServiceItemVO>> queryServiceItemOfVenue(@RequestBody ServiceItemQuery serviceItemQuery) {
        log.info("queryServiceItemOfTransport - 场馆现场预订服务的服务列表 serviceItemQuery={}", serviceItemQuery);
        return R.success(baseService.queryServiceItemOfVenue(serviceItemQuery));
    }

    /**
     * 同步服务
     *
     * @param serviceItemDTO 同步服务
     * @return R
     */
    @ApiOperation(value = "同步服务", notes = "同步服务")
    @PostMapping("/syncServiceItem")
    public R<Boolean> syncServiceItem(@RequestBody ServiceItemDTO serviceItemDTO) {
        log.info("syncServiceItem - 同步服务 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.syncServiceItem(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 导出服务.
     *
     * @param serviceItemDTO serviceItemDTO
     * @param response          the response
     * @throws Exception the exception
     */
    @PostMapping("/exportServiceItem")
    public void exportServiceItem(@RequestBody ServiceItemDTO serviceItemDTO, HttpServletResponse response) throws Exception {
        log.info("exportServiceItem - 导出服务, serviceItemDTO={}", serviceItemDTO);
        baseService.exportServiceItem(serviceItemDTO, response);
    }

}